﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace FinMkts.Messaging
{
    [InheritedExport]
    public abstract class Subscriber : IDisposable, IObservable<AddressedMessage>
    {
        protected Subject<AddressedMessage> Subject { get; private set; }

        public Subscriber()
        {
            Subject = new Subject<AddressedMessage>();
        }

        public abstract void Start();

        public abstract void Stop();

        public void Dispose()
        {
            try
            {
                Stop();
            }
            catch
            {
                // dispose should never throw an exception
            }
            GC.SuppressFinalize(this);
        }

        public IDisposable Subscribe(IObserver<AddressedMessage> observer)
        {
            return Subject.Subscribe(observer);
        }
    }
}
